{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-19T18:09:35.712808Z",
     "start_time": "2020-05-19T18:09:33.401328Z"
    }
   },
   "outputs": [
    {
     "data": {
      "application/javascript": [
       "\n",
       "require(['notebook/js/codecell'], function(codecell) {\n",
       "  codecell.CodeCell.options_default.highlight_modes[\n",
       "      'magic_text/x-csrc'] = {'reg':[/^%%microblaze/]};\n",
       "  Jupyter.notebook.events.one('kernel_ready.Kernel', function(){\n",
       "      Jupyter.notebook.get_cells().map(function(cell){\n",
       "          if (cell.cell_type == 'code'){ cell.auto_highlight(); } }) ;\n",
       "  });\n",
       "});\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import time\n",
    "from pynq import Overlay\n",
    "import asyncio\n",
    "\n",
    "gpio_inter_design = Overlay(\"./data/3.AXI_GPIO_Interrupt.bit\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-19T18:09:35.815764Z",
     "start_time": "2020-05-19T18:09:35.722364Z"
    }
   },
   "outputs": [],
   "source": [
    "button_inter = gpio_inter_design.axi_gpio_0.Input( parent=gpio_inter_design.axi_gpio_0.channel1, start=0, stop=1 )\n",
    "led = gpio_inter_design.axi_gpio_0.Output( parent=gpio_inter_design.axi_gpio_0.channel2, start=0, stop=1 )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-19T17:44:22.977992Z",
     "start_time": "2020-05-19T17:44:22.260389Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "中断的周期 ：0.6854667663574219s\n",
      "中断的周期 ：6.651878356933594e-05s\n",
      "中断的周期 ：5.888938903808594e-05s\n",
      "中断的周期 ：5.817413330078125e-05s\n",
      "中断的周期 ：5.888938903808594e-05s\n",
      "中断的周期 ：5.745887756347656e-05s\n",
      "中断的周期 ：5.745887756347656e-05s\n",
      "中断的周期 ：5.793571472167969e-05s\n",
      "中断的周期 ：5.817413330078125e-05s\n",
      "中断的周期 ：5.8650970458984375e-05s\n"
     ]
    }
   ],
   "source": [
    "async def button_led():\n",
    "    for i in range(10):\n",
    "        start = time.time()\n",
    "        await button_inter.wait_for_value_async(1)\n",
    "        led.toggle()\n",
    "#         await asyncio.sleep(0.05)\n",
    "        end = time.time()\n",
    "        \n",
    "        print(\"中断的周期 ：{}s\".format(end -start))\n",
    "    \n",
    "\n",
    "start_loop = asyncio.get_event_loop()\n",
    "return_value = start_loop.run_until_complete(button_led())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-19T18:11:30.605501Z",
     "start_time": "2020-05-19T18:11:21.449451Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "中断的周期 ：0.1248316764831543s\n",
      "中断的周期 ：0.9994022846221924s\n",
      "中断的周期 ：0.9997074604034424s\n",
      "中断的周期 ：0.9996352195739746s\n",
      "中断的周期 ：0.999701738357544s\n",
      "中断的周期 ：0.9996013641357422s\n",
      "中断的周期 ：0.9996085166931152s\n",
      "中断的周期 ：1.0001087188720703s\n",
      "中断的周期 ：0.9996082782745361s\n",
      "中断的周期 ：0.9993538856506348s\n"
     ]
    }
   ],
   "source": [
    "async def button_led():\n",
    "    for i in range(10):\n",
    "        start = time.time()\n",
    "        await button_inter.wait_for_value_async(1)\n",
    "        led.toggle()\n",
    "        await asyncio.sleep(0.05)\n",
    "        end = time.time()\n",
    "        \n",
    "        print(\"中断的周期 ：{}s\".format(end -start))\n",
    "    \n",
    "\n",
    "start_loop = asyncio.get_event_loop()\n",
    "return_value = start_loop.run_until_complete(button_led())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
